package com.yilife.hmstr.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.yilife.hmstr.domain.MeridianChangeAnalysis;
import com.yilife.hmstr.domain.TestReport;
import com.yilife.hmstr.domain.TrProblem;
import com.yilife.hmstr.domain.tr;
import com.yilife.hmstr.mapper.MeridianChangeAnalysisMapper;
import com.yilife.hmstr.mapper.TestReportMapper;
import com.yilife.hmstr.mapper.TrProblemMapper;
import com.yilife.hmstr.service.TestReportService;

/**
 * 检测报告业务实现类
 */
@Service
public class TestReportServiceImpl extends ServiceImpl<TestReportMapper, TestReport> implements TestReportService{
	
	@Autowired
	private TestReportMapper testReportMapper;
	
	@Autowired
	private MeridianChangeAnalysisMapper meridianChangeAnalysisMapper;
	
	@Autowired
	private TrProblemMapper trProblemMapper;

	/**
	 * 级联添加TestReport数据
	 * @param testReport
	 * @param meridianChangeAnalysis
	 * @param trProblems
	 */
	public void add(TestReport testReport,List<MeridianChangeAnalysis> meridianChangeAnalysis,List<TrProblem> trProblems){
//		System.out.print("检查报告"+testReportMapper.selectById(testReport.getHmstrId()));
//		if(testReport!=null&&testReportMapper.selectById(testReport.getHmstrId())==null){
//		System.out.print("是否为空"+testReportService.selectByPid(testReport.getHmstrId()).getTestReport());
//		System.out.print("是否为空"+testReport.getHmstrId());
		if(testReport!=null&&selectByPid(testReport.getHmstrId()).getTestReport()==null){
			testReportMapper.insert(testReport);
			for (TrProblem trProblem : trProblems) {
				trProblemMapper.insert(trProblem);
			}
			for (MeridianChangeAnalysis mca : meridianChangeAnalysis) {
				meridianChangeAnalysisMapper.insert(mca);
			}
		}
		else{}
//		testReportMapper.insert(testReport);
//			for (TrProblem trProblem : trProblems) {
//				trProblemMapper.insert(trProblem);
//			}
//			for (MeridianChangeAnalysis mca : meridianChangeAnalysis) {
//				meridianChangeAnalysisMapper.insert(mca);
//			}
	}
	
	/**
	 * 级联删除TestReport数据
	 * @param id
	 */
	public void del(long id){
		meridianChangeAnalysisMapper.delete(new EntityWrapper<MeridianChangeAnalysis>().eq("tr_id", id));
		trProblemMapper.delete(new EntityWrapper<TrProblem>().eq("tp_id", id));
		testReportMapper.deleteById(id);
	}
	
	/**
	 *更新TestReport数据
	 */
	public void update(TestReport testReport){
		testReportMapper.updateById(testReport);
	}
	
	/**
	 * 根据外键删除数据
	 * @param id
	 */
	public void delByPid(long id){
		List<TestReport> testReports=new TestReport().selectList(new EntityWrapper<TestReport>().eq("hmstr_id", id));
		for (TestReport testReport : testReports) {
			del(testReport.getTrId());
		}
	}
	
	/**
	 * 级联查询
	 * @return
	 */
	/*
	public String selectById(int id){
		String str="";
		List<TestReport> testReports=new TestReport().selectList(new EntityWrapper<TestReport>().eq("tr_id", id));
		List<MeridianChangeAnalysis> msAnalysis=new MeridianChangeAnalysis().selectList(new EntityWrapper<MeridianChangeAnalysis>().eq("tr_id", id));
		List<TrProblem> trProblems=new TrProblem().selectList(new EntityWrapper<TrProblem>().eq("tp_id", id));
		str=str+testReports.toString();
		for (MeridianChangeAnalysis meridianChangeAnalysis : msAnalysis) {
			str=str+"\n\n\r\r"+meridianChangeAnalysis.toString();
		}
		for (TrProblem trProblem : trProblems) {
			str=str+"\n\n\r\r"+trProblem.toString();
		}
		return str;
	}
	
	public String selectByPid(int pid){
		String string="";
		List<TestReport> trs=testReportMapper.selectList(new EntityWrapper<TestReport>().eq("hmstr_id", pid));
		for (TestReport testReport : trs) {
			string=string+"\n\n\r\r"+selectById(testReport.getTrId());
		}
		return string;
	}
	*/

	/**
	 * 根据主键查询数据
	 * @param id
	 * @return
	 */
	public tr selectById(long id){
		List<TestReport> testReports=new TestReport().selectList(new EntityWrapper<TestReport>().eq("tr_id", id));
		List<MeridianChangeAnalysis> msAnalysis=new MeridianChangeAnalysis().selectList(new EntityWrapper<MeridianChangeAnalysis>().eq("tr_id", id));
		List<TrProblem> trProblems=new TrProblem().selectList(new EntityWrapper<TrProblem>().eq("tp_id", id));
		return new tr(testReports.size()!=0?testReports.get(0):null,trProblems.size()==0?null:trProblems, msAnalysis.size()==0?null:msAnalysis);

	}

	/**
	 * 根据外键查询数据
	 * @param pid
	 * @return
	 */
	public tr selectByPid(long pid){
		List<TestReport> trs=testReportMapper.selectList(new EntityWrapper<TestReport>().eq("hmstr_id", pid));
//		System.out.print("trs为空吗"+trs==null);
		return selectById(trs.size()!=0?trs.get(0).getTrId():0);
	}

}
